﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using RemoteObject;
using System.Collections;

namespace RemoteClient.UI
{
    public partial class SegmentListView : UserControl
    {
        public SegmentListView()
        {
            InitializeComponent();
        }

        public void AddSegment(DownloadWorkder segment)
        {
            segmentList.Items.Add(new SegmentItem(segment));
        }

        public void AddSegments(List<SegmentItem> segments)
        {
            foreach (SegmentItem seg in segments)
            {
                Console.WriteLine("Add Segment :" + seg.ToString());            
                segmentList.Items.Add(seg);
            }            
        }

        public void RemoveSegment(DownloadWorkder segment)
        {
            IEnumerator itemEnumerator = segmentList.Items.GetEnumerator();
            SegmentItem segItem = null;

            while (itemEnumerator.MoveNext())
            {
                SegmentItem item = (SegmentItem)itemEnumerator.Current;
                if (item.Downloader.Equals(segment))
                {
                    segItem = item;
                    break;
                }
            }

            if (segItem != null)
            {
                segItem.Dispose();
                segmentList.Items.Remove(segItem);
            }
        }

        public void RemoveAllSegments()
        {
            foreach(SegmentItem item in segmentList.Items)
            {
                item.Dispose();
            }
            segmentList.Items.Clear();
        }
    }
}
